package com.zzyl.nursing.service.impl;

import java.util.List;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertQueryDto;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Objects;

/**
 * 报警数据Service业务层处理
 * 
 * @author laoYe
 * @date 2025-11-01
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 处理报警数据
     * @param handleAlertDataDto
     */
    @Override
    public void handleAlertData(HandleAlertDataDto handleAlertDataDto) {
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userName = loginUser.getUser().getUserName();
        long userId = loginUser.getUserId();
        if (Objects.isNull(userName) || Objects.isNull(userId)){
            throw new ServiceException("用户不存在");
        }
        // 查询报警数据
        AlertData alertData = selectAlertDataById(handleAlertDataDto.getId());
        // 处理报警数据
        alertData.setProcessingResult(handleAlertDataDto.getProcessingResult());
        alertData.setProcessingTime(handleAlertDataDto.getProcessingTime());
        alertData.setUserId(userId);
        alertData.setProcessorName(userName);
        alertData.setStatus(1);
        //更新报警数据
        updateAlertData(alertData);
    }

    /**
     * 分页查询报警数据列表
     *
     * @param dto 报警数据
     * @return 报警数据
     */
    @Override
    public TableDataInfo<AlertData> pageList(AlertQueryDto dto) {
        //设置分页参数
        Page<AlertData> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        //构建查询条件
        lambdaQuery()
                .like(StrUtil.isNotEmpty(dto.getDeviceName()), AlertData::getDeviceName, dto.getDeviceName())
                .ge(dto.getStartTime() != null,AlertData::getCreateTime, dto.getStartTime())
                .le(dto.getEndTime() != null,AlertData::getCreateTime, dto.getEndTime())
                .eq(dto.getStatus() != null,AlertData::getStatus, dto.getStatus())
                .orderByDesc(AlertData::getCreateTime)
                .page(page);
        log.info("分页查询报警数据列表：{}, {}", page.getPages(), page.getRecords());

        return new TableDataInfo<>(page.getTotal(), page.getRecords(),200, "查询成功");
    }
}
